package android.tusnotas.model;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.tusnotas.beans.Course;

public class CourseModel {

	
//INSERTS
	public long insertCourse(String name, String classroom, Integer instanceId, String days, String timetable, String examenDate, String idAula, String attendance) {
		TusNotasModel.courseInsertStmt.bindString(1, name);
		TusNotasModel.courseInsertStmt.bindString(2, classroom);
		TusNotasModel.courseInsertStmt.bindDouble(3, instanceId);
		TusNotasModel.courseInsertStmt.bindString(4, days);
		TusNotasModel.courseInsertStmt.bindString(5, timetable);
		TusNotasModel.courseInsertStmt.bindString(6, examenDate);
		TusNotasModel.courseInsertStmt.bindString(7, idAula);
		TusNotasModel.courseInsertStmt.bindString(8, attendance);
		return TusNotasModel.courseInsertStmt.executeInsert();
	}
	
	public long insertCourse(Course course) {
		TusNotasModel.courseInsertStmt.bindString(1, course.getName());
		TusNotasModel.courseInsertStmt.bindString(2, course.getClassroom());
		TusNotasModel.courseInsertStmt.bindDouble(3, course.getInstanceId());
		TusNotasModel.courseInsertStmt.bindString(4, course.getDays());
		TusNotasModel.courseInsertStmt.bindString(5, course.getTimetable());
		TusNotasModel.courseInsertStmt.bindString(6, course.getExamenDate());
		TusNotasModel.courseInsertStmt.bindString(7, course.getIdAula());
		TusNotasModel.courseInsertStmt.bindString(8, course.getAttendance());
		try{
			return TusNotasModel.courseInsertStmt.executeInsert();
		}catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
//END INSERTS
	
//DELETES	
	public void deleteAllCourses() {
		TusNotasModel.db.delete(TusNotasModel.COURSE_TABLE_NAME, null, null);
	}
	
	public void deleteCourseById(Integer id) {
		TusNotasModel.db.delete(TusNotasModel.COURSE_TABLE_NAME, "id = "+id, null);
	}
//END DELETES
	
//SELECTS 	
	public List<Course> selectAllCourses() {
		List<Course> courses = new ArrayList<Course>();
		Course course = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.COURSE_TABLE_NAME, new String[] { "id", "name", "classroom", "instanceId", "days", "timetable", "examenDate", "idAula", "attendance" },
				null, null, null, null, "id ASC");
		
		if (cursor.moveToFirst()) {
			do {
				course = new Course();
				course.setId(cursor.getInt(0));
				course.setName(cursor.getString(1));
				course.setClassroom(cursor.getString(2));
				course.setInstanceId(cursor.getInt(3));
				course.setDays(cursor.getString(4));
				course.setTimetalbe(cursor.getString(5));
				course.setExamenDate(cursor.getString(6));
				course.setIdAula(cursor.getString(7));
				course.setAttendance(cursor.getString(8));
				courses.add(course);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return courses;
	}

	
	public List<Course> selectCoursesByInstance(Integer instanceId) {
		List<Course> courses = new ArrayList<Course>();
		Course course = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.COURSE_TABLE_NAME, new String[] { "id", "name", "classroom", "instanceId", "days", "timetable", "examenDate", "idAula", "attendance" },
				"instanceId ='" + instanceId +"'", null, null, null, "id ASC");
		
		if (cursor.moveToFirst()) {
			do {
				course = new Course();
				course.setId(cursor.getInt(0));
				course.setName(cursor.getString(1));
				course.setClassroom(cursor.getString(2));
				course.setInstanceId(cursor.getInt(3));
				course.setDays(cursor.getString(4));
				course.setTimetalbe(cursor.getString(5));
				course.setExamenDate(cursor.getString(6));
				course.setIdAula(cursor.getString(7));
				course.setAttendance(course.getAttendance());
				courses.add(course);
				
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return courses;
	}
	
	public Course selectCourseById(Integer courseId) {
		Course course = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.COURSE_TABLE_NAME, new String[] { "id", "name", "classroom", "instanceId", "days", "timetable", "examenDate", "idAula", "attendance" },
				"id = " + courseId , null, null, null, null, "1");
		
		if (cursor.moveToFirst()) {
			do {
				course = new Course();
				course.setId(cursor.getInt(0));
				course.setName(cursor.getString(1));
				course.setClassroom(cursor.getString(2));
				course.setInstanceId(cursor.getInt(3));
				course.setDays(cursor.getString(4));
				course.setTimetalbe(cursor.getString(5));
				course.setExamenDate(cursor.getString(6));
				course.setIdAula(cursor.getString(7));
				course.setAttendance(cursor.getString(8));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return course;
	}
	
	public Course selectCourseByIdAula(Integer idAula) {
		Course course = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.COURSE_TABLE_NAME, new String[] { "id", "name", "classroom", "instanceId", "days", "timetable", "examenDate", "idAula", "attendance" },
				"idAula = " + idAula , null, null, null, null, "1");
		
		if (cursor.moveToFirst()) {
			do {
				course = new Course();
				course.setId(cursor.getInt(0));
				course.setName(cursor.getString(1));
				course.setClassroom(cursor.getString(2));
				course.setInstanceId(cursor.getInt(3));
				course.setDays(cursor.getString(4));
				course.setTimetalbe(cursor.getString(5));
				course.setExamenDate(cursor.getString(6));
				course.setIdAula(cursor.getString(7));
				course.setAttendance(cursor.getString(8));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return course;
	}
	
	public Course selectCourseByInstanceAndName(Integer instanceId, String name) {
		Course course = null;
		Cursor cursor = TusNotasModel.db.query(TusNotasModel.COURSE_TABLE_NAME, new String[] { "id", "name", "classroom", "instanceId", "days", "classroom", "examenDate", "idAula", "attendance" },
				"instanceId = " + instanceId +" and name = '" + name +"'", null, null, null, "id ASC", "1");
		
		if (cursor.moveToFirst()) {
			do {
				course = new Course();
				course.setId(cursor.getInt(0));
				course.setName(cursor.getString(1));
				course.setClassroom(cursor.getString(2));
				course.setInstanceId(cursor.getInt(3));
				course.setDays(cursor.getString(4));
				course.setTimetalbe(cursor.getString(5));
				course.setExamenDate(cursor.getString(6));
				course.setIdAula(cursor.getString(7));
				course.setAttendance(cursor.getString(8));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return course;
	}
}
//END SELECTS
